﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace POSPlus.DatabaseOperations
{
	/// <summary>
	/// Manages a connection with a database.  Alows the ability to change the connection,
	/// disconnect and reconnect.
	/// </summary>
	/// <remarks>
	/// This class is depreciated: Extend the class DatabaseObject for the specific
	/// information you need from the database.
	/// </remarks>
	public class DatabaseConnectionManager
	{
		#region Properties
		public String Server 
		{
			get
			{
				return _server;
			}
			set
			{
				_server = value;
				UpdateConnectionString();
			} 
		}

		public String User
		{
			get
			{
				return _user;
			}
			set
			{
				_user = value;
				UpdateConnectionString();
			}
		}
		public String Password
		{
			get
			{
				return _password;
			}
			set
			{
				_password = value;
				UpdateConnectionString();
			}
		}
		public String Database
		{
			get
			{
				return _database;
			}
			set
			{
				_database = value;
				UpdateConnectionString();
			}
		}
		public SqlConnection Connection { get; private set; }
		#endregion

		#region Members
		private String _sqlConnectionString;
		private String _server;
		private String _user;
		private String _password;
		private String _database;
		private string connectionString;
		#endregion

		#region Constructors
		/// <summary>
		/// Default constructor creates a new instance of SqlConntion with the given
		/// Server, User, Password and Database.
		/// </summary>
		/// <param name="server"></param>
		/// <param name="user"></param>
		/// <param name="password"></param>
		/// <param name="database"></param>
		public DatabaseConnectionManager(String server, String user, String password, String database)
		{
			_sqlConnectionString = String.Format(
				"Data Source={0};User ID={1};Password={2};Initial Catalog={3};", 
				server, user, password, database);

			Connection = new SqlConnection(_sqlConnectionString);

			Server = server;
			User = user;
			Password = password;
			Database = database;
		}

		public DatabaseConnectionManager(string connectionString)
		{
			_sqlConnectionString = connectionString;
			Connection = new SqlConnection(_sqlConnectionString);
			String[] splitString = _sqlConnectionString.Split(';');
			Server = splitString[0].Split('=')[1];
			User = splitString[1].Split('=')[1];
			Password = splitString[2].Split('=')[1];
			Database = splitString[3].Split('=')[1];
		}
		#endregion

		#region Methods
		/// <summary>
		/// Open the Connection
		/// </summary>
		/// 
		/// <exception cref="InvalidOperationException"/>
		/// <exception cref="SqlException"/>
		public void Open()
		{
				Connection = new SqlConnection(_sqlConnectionString);

			try
			{
				Connection.Open();
			}
			catch (InvalidOperationException)
			{
				throw;
			}
			catch (SqlException)
			{
				throw;
			}
		}

		/// <summary>
		/// Close the connection.
		/// </summary>
		/// 
		/// <exception cref="SqlException"/>
		public void Close()
		{
			try
			{
				Connection.Close();
			}catch(SqlException)
			{
				throw;
			}
		}

		/// <summary>
		/// Updates the connection string to the currently set values. Then
		/// recreates the Connection object with the new values.
		/// </summary>
		/// <exception cref="InvalidOperationException">
		/// Thrown if the connection is still open.
		/// </exception>
		private void UpdateConnectionString()
		{
			if (Connection.State != ConnectionState.Closed)
				throw new InvalidOperationException(
					"Cannot change SQL Connection while connection is open.");

			_sqlConnectionString = String.Format(
				"Data Source={0};User ID={1};Password={2};Initial Catalog={3};",
				_server, _user, _password, _database);

			Connection.Dispose();
			Connection = new SqlConnection(_sqlConnectionString);
		}

		public override string ToString()
		{
			return _sqlConnectionString;
		}
		#endregion
	}
}
